<?php
	//Get todays date
	$today = getdate();
	echo date('g:i.s A')."<br/>";
	
	//connect to mysql
	$objConnect = mysql_connect("localhost","root","") or die(mysql_error());
	$objDB = mysql_select_db("Project 5.5");
	
	$sql = "SELECT u.User_ID, u.First_Name, u.Last_Name, u.Email, s.Class_ID, s.Group_ID FROM users u, studentinfo s WHERE u.User_ID=s.User_ID";
	$result = mysql_query($sql);
	
	$i=0;
	while ($row = mysql_fetch_row($result)) {
		$students[$i]= $row;
		$i++;
	}
	
	$sql = "SELECT Assessment_ID, Due_Date, Submission, Assessment_Name FROM assessment";
	$result = mysql_query($sql);
	
	$i=0;
	while ($row = mysql_fetch_row($result)) {
		$assessment[$i]= $row;
		$i++;
	}
	
	foreach ($students as $s) {
		foreach ($assessment as $a) {
			$due=explode("/", $a[1]);
			$predue="";
			//if past due date
			if (($today['year']>$due[2]) || ($today['year']>=$due[2] && $today['mon']>$due[1]) || ($today['year']>=$due[2] && $today['mon']>=$due[1] && $today['mday']>$due[0])) {
				$sql = "SELECT * FROM results WHERE Student_ID='".$s[0]."' AND Assessment_ID='".$a[0]."'";
				$result = mysql_query($sql);
				$count = mysql_num_rows($result);
				//send late email to student if they havent submitted it yet
				if ($count==0) {
					sendEmail($s, $a, "Late");
				}
				
			} else {	//assignment not past due date
				//calculate 3 days before due date
				if ($due[0]>3) {
					$predue = ($due[0]-3)."/".$due[1]."/".$due[2];
				} else {
					switch($due[1]) {
						case 1:
							$predue = (31+($due[0]-3))."/12/".($due[2]-1);
							$break;
						case 2:
						case 4:
						case 6:
						case 8:
						case 9:
						case 11:
							$predue = (31+($due[0]-3))."/".($due[1]-1)."/".$due[2];
							$break;
						case 5:
						case 7:
						case 10:
						case 12:
							$predue = (30+($due[0]-3))."/".($due[1]-1)."/".$due[2];
							$break;
						case 3 :
							//deal with leap years and feb
							if (($due[2]%4==0)&&(($due[2]%100!=0)||($due[2]%400==0))) {
								$predue = (29+($due[0]-3))."/".($due[1]-1)."/".$due[2];
							} else {
								$predue = (28+($due[0]-3))."/".($due[1]-1)."/".$due[2];
							}
							$break;
					}
				}
				//if its 3 days before due, email student
				if ($predue==date('j/m/Y')) {
					sendEmail($s,$a,"Due");
				}	
			}
		}
		
	}
	
	//send email to student about assignment
	function sendEmail($s, $a, $state) {
		$subject = "Warning - ".$a[3]." ".$state;
		$message="";
		switch ($state) {
			case "Due":
				$message = "Dear ".$s[1].",\n\nThe assessment item '".$a[3]."' is due on the ".$a[1].".\nPlease submit this assessment item by the due date.\nThank you";
				break;
			case "Late":
				$message = "Dear ".$s[1].",\n\nThe assessment item '".$a[3]."' was not submited by its due date (".$a[1].").\nPlease submit this assessment item as soon as possible as marks will be deducted.\nThank you";
				break;
		}
		$sender="From: admin@qut.edu.au";
		if (mail($s[3], $subject, $message, $sender)) {
			echo "Sent email to ".$s[3]." ".$a[3]." is ".$state.".<br/>";
		} else {
			echo "Failed to send email to ".$s[3]." ".$a[3]." is ".$state.".<br/>";
		}
	}
	
?>